/*
 * Copyright (c) 2010-2011 NOO. All Rights Reserved.
 * [Id:DefDepartmentCmp.java  2011-11-03 下午8:46 poplar.yfyang ]
 */
package org.mumu.pawn.cmps.def;

import org.jboss.logging.Logger;
import org.joda.time.DateTimeUtils;
import org.mumu.pawn.beans.common.AccountSessionBean;
import org.mumu.pawn.cmps.DepartmentCmp;
import org.mumu.pawn.datamodal.EntityDataModalFactory;
import org.mumu.pawn.fm.dm.LazyPawnDataModel;
import org.mumu.pawn.domain.Department;

import javax.inject.Inject;
import javax.persistence.EntityManager;

/**
 * <p>
 * 部门操作的具体实现.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2011-11-03 下午8:46
 * @since JDK 1.5
 */
public class DefDepartmentCmp implements DepartmentCmp {
    private static final long serialVersionUID = 8819214889224747867L;
    @Inject
    private EntityManager entityManager;

    @Inject
    private AccountSessionBean accountSessionBean;
    @Inject
    private Logger logger;

    @Override
    public void saveOrUpdateDepartment(Department department) {
        if (department.getId() == 0) {
            logger.info("新增部门：" + department.getDepName());
            department.setCreateActor(accountSessionBean.getAccount().getId());
            department.setCreateTime(DateTimeUtils.currentTimeMillis());
            entityManager.persist(department);
        } else {
            logger.info("修改部门：" + department.getDepName());
            department.setUpdateActor(accountSessionBean.getAccount().getId());
            department.setUpdateTime(DateTimeUtils.currentTimeMillis());
            entityManager.merge(department);
        }
    }

    @Override
    public void deleteDepartment(long id) {
        logger.info("删除部门：" + id);
        Department department = getDepartment(id);
        entityManager.remove(department);
    }

    @Override
    public Department getDepartment(long id) {
        logger.info("查询部门：" + id);
        return entityManager.find(Department.class, id);
    }

    @Override
    public LazyPawnDataModel createDepartmentDataModal() {
        return EntityDataModalFactory.builderDataModal(entityManager,Department.class);
    }
}
